<template>
  <div class="test">
    test
  </div>
</template>

<script>
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'test',

  setup() {
    const initWebSocket = () => {
      let websocket = new WebSocket("ws://localhost:8888/api/v1/maven/run/test-maven")

      //连接发生错误的回调方法
      websocket.onerror = () => {
        console.log('error')
      }

      //连接成功建立的回调方法
      websocket.onopen = () => {
        console.log('连接建立成功')
      }

      //接受到消息的回调方法
      websocket.onmessage = (event) => {
        console.log(event.data)
      }

      //连接关闭的回调方法
      websocket.onclose = () => {
        console.log('连接关闭')
      }

      //监听窗口关闭事件， 当窗口关闭时，主动去关闭websocket链接, 防止链接还没断开就关闭窗口, server端会炮异常
      window.onbeforeunload = () => {
        websocket.close();
      }

      // function send() {
      //   var message = "消息接收success"
      //   websocket.send(message)
      // }
    }
    initWebSocket()
    return {

    }
  }
})
</script>

<style lang="scss" scoped>

</style>